#include "stdafx.h"
#include "Arrow.h"



CArrow::CArrow(void)
{
}

CArrow::CArrow(CPoint pBegin, CPoint pEnd, COLORREF cPen)
{
	this->m_pBegin	= m_pMax = m_pMin = pBegin;
	this->m_pEnd	= pEnd;
	m_nAngle = NUM_OF_ANGLE;
	m_pCoordinates = new CPoint[NUM_OF_ANGLE];
	this->m_cPenColor = cPen;
	m_nPenStyle = PS_SOLID;
	m_nPenWidth = 2;
	m_bSelected = FALSE;
	
}

CArrow::CArrow (CPoint m_pBegin, CPoint m_pEnd, COLORREF m_clrBack, COLORREF m_cPenColor, BASESHAPE__T m_nShapeType, INT m_nRotate)
{
	this->m_nRotate = m_nRotate;
	this->m_pBegin = m_pBegin;
	this->m_pEnd = m_pEnd;
	this->m_clrBack = m_clrBack;
	this->m_cPenColor = m_cPenColor;
	this->m_nShapeType = m_nShapeType;
	m_brushColor.CreateSolidBrush(RGB(255,255,255));
//	this->m_szText = m_szText;
//	this->m_color = m_color;
	m_nAngle = NUM_OF_ANGLE;
	m_pCoordinates = new CPoint[NUM_OF_ANGLE];
	m_nPenStyle = PS_SOLID;
	m_nPenWidth = 2;
}

void CArrow::Serialize (CArchive &ar)
{
	if (ar.IsStoring() )
	{
		ar << m_pBegin << m_pEnd << m_clrBack << m_cPenColor << m_nShapeType << m_szText << m_color << m_nRotate;
	}
	else
	{
		int nShapeType;
		ar >> m_pBegin >> m_pEnd >> m_clrBack >> m_cPenColor >> nShapeType >> m_szText >> m_color >> m_nRotate;
		m_nShapeType = (BASESHAPE__T)nShapeType;
	}
}

CArrow::~CArrow(void)
{
}

BOOL CArrow::Draw(CDC *cDC)
{
	CPen cPen(m_nPenStyle, m_nPenWidth, m_cPenColor);
	radiansOffset = DegreesToRadians(AngleDist() + m_nRotate);
	CPen *pOldPen = cDC->SelectObject(&cPen);
	this->setCoordinate();
	cDC->MoveTo(m_pCoordinates[0]);
	cDC->LineTo(m_pCoordinates[1]);

	cDC->MoveTo(m_pCoordinates[1]);
	cDC->LineTo(m_pCoordinates[2]);

	cDC->MoveTo(m_pCoordinates[1]);
	cDC->LineTo(m_pCoordinates[3]);
	if(m_bSelected == TRUE)
		this->DrawTracker(cDC);
	cDC->SelectObject(pOldPen);
	pOldPen->DeleteObject();
	return FALSE;
}

void CArrow::setCoordinate()
{
	CPoint Center;
	const double radius = Radius;
	m_pCoordinates[1] = m_pEnd;
	 m_pMax = m_pMin = m_pBegin;
	Center.x = m_pEnd.x - 13 * cos(radiansOffset + 0 * 2 * PI / 3);
	Center.y = m_pEnd.y - 13 * sin(radiansOffset + 0 * 2 * PI / 3);
	for( int i=1; i < 3; ++i )
	{
		const double angle = radiansOffset + i * 2 * PI / 3;
		m_pCoordinates[i + 1].x = 2 * m_pCoordinates[1].x - Center.x + 7 * cos(angle);
		m_pCoordinates[i + 1].y = 2 * m_pCoordinates[1].y - Center.y + 7 * sin(angle);
	}
	m_pCoordinates[0]  = m_pBegin;
	for(int i = 0; i < m_nAngle; i++)
	{
		m_pMin.x = min(m_pMin.x, m_pCoordinates[i].x);
		m_pMin.y = min(m_pMin.y, m_pCoordinates[i].y);
		m_pMax.x = max(m_pMax.x, m_pCoordinates[i].x);
		m_pMax.y = max(m_pMax.y, m_pCoordinates[i].y);

	}
}

void CArrow::DrawTracker(CDC *cDC)
{
	const unsigned int Width = 4;
	CRect m_rectT(CPoint(m_pBegin.x - Width, m_pBegin.y - Width), CPoint(m_pBegin.x + Width, m_pBegin.y + Width));
	CPen cPen(m_nPenStyle, 1, m_cPenColor);
	cDC->SelectObject(&cPen);
	cDC->Rectangle(CRect(CPoint(m_pBegin.x - Width, m_pBegin.y - Width), CPoint(m_pBegin.x + Width, m_pBegin.y + Width)));
	cDC->Rectangle(CRect(CPoint(m_pEnd.x - Width, m_pEnd.y - Width), CPoint(m_pEnd.x + Width, m_pEnd.y + Width)));
}

int CArrow::AngleDist()
{
	//he so goc cua duong thang
	INT A, B; 
	//goc cua duong thang
	float Angle;
	//tinh he so cua duong thang
	A = m_pEnd.y - m_pBegin.y;
	B = m_pBegin.x - m_pEnd.x;
	//tinh goc
	Angle = B / sqrtf((float)A * A + B * B);
	if(m_pEnd.y > m_pBegin.y)
		return 360 - acos(Angle) * 180.0 / PI;
	return acos(Angle) * 180.0 / PI;
}